package com.yuke.cloud.service.opc.mq.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.base.constant.MQConstants;
import com.yuke.cloud.common.core.mq.AbstractMessageListener;
import com.yuke.cloud.service.opc.mq.consumer.OpcSendEmailConsumer;
import com.yuke.cloud.service.opc.mq.consumer.OpcSendSmsConsumer;
import com.yuke.cloud.service.opc.service.OpcSendNotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class OpcMessageListener extends AbstractMessageListener {

    @Resource
    private OpcSendSmsConsumer opcSendSmsConsumer;
    @Resource
    private OpcSendEmailConsumer opcSendEmailConsumer;
    @Resource
    private OpcSendNotifyService opcSendNotifyService;

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        Object bizObj = messageConverter.fromMessage(message);  //json字符串
        //logger.info("get message success:" + bizObj.toString());

        JSONObject jsonObject = JSON.parseObject(bizObj.toString());

        String msgType = jsonObject.getString("msgType");

        if (msgType.equals(MQConstants.MQ_OPC_SEND_EMAIL)) {
            opcSendEmailConsumer.handlerSendEmail(bizObj.toString());
        } else if (msgType.equals(MQConstants.MQ_OPC_SEND_SMS)) {
            opcSendSmsConsumer.handlerSendSms(bizObj.toString());
        } else if (msgType.equals(MQConstants.MQ_OPC_SEND_NOTIFY)){
            //发送通知给app你购买的商品发货了
//            opcSendNotifyService.sendNotifyByUser(jsonObject.get("d").toString(), jsonObject.get("a").toString(),"");
        }
    }
}
